﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CuteEditor;
using Wjtour.Domain;
using Wjtour.BLL;
using Hjp.FrameWork.Common;

namespace Wjtour.Web.Admin.traffic
{
    public partial class addedi : BasePage
    {
        protected DropDownList cid;
        protected TextBox title;
        protected TextBox img;
        protected TextBox source;
        protected TextBox keyword;
        protected TextBox desc;
        protected TextBox sort;
        protected Editor content;
        protected Button submit;
        protected string type;

        protected int id = 0;
        protected int categoryid = 0;
        Wjtour.Domain.traffic link = new Domain.traffic();
        traffic_bll _bll = new traffic_bll();

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Utils.StrToInt(Request["id"], 0);
            categoryid = Utils.StrToInt(Request["cid"], 0);
            type = Request["type"];
            submit.Click += new EventHandler(submit_Click);
            if (!IsPostBack)
            {
                BindDropdown();
            }
            if (id > 0)
            {
                link = _bll.Load(id);

                if (!IsPostBack)
                {
                    if (link != null)
                    {
                        title.Text = link.title;
                        cid.SelectedValue = link.cid.ToString();
                        source.Text = link.source;
                        img.Text = link.img;
                        keyword.Text = link.keyword;
                        desc.Text = link.desc;
                        sort.Text = link.sort.ToString();
                        content.Text = link.content;
                    }
                }
            }
            if (categoryid > 0)
            {
                cid.SelectedValue = categoryid.ToString();
            }
        }

        void BindDropdown()
        {
            cid.Items.Add(new ListItem("城市地图", "1"));
            cid.Items.Add(new ListItem("公交线路", "2"));
            cid.Items.Add(new ListItem("出租车", "3"));
            cid.Items.Add(new ListItem("周边机场", "4"));
        }

        void submit_Click(object sender, EventArgs e)
        {
            link.id = id;
            link.title = title.Text;
            link.cid = Utils.StrToInt(cid.SelectedValue, 0);
            link.source = source.Text;
            link.img = img.Text;
            link.keyword = keyword.Text;
            link.desc = desc.Text;
            link.sort = Utils.StrToInt(sort.Text, 0);
            link.content = content.Text;
            if (link.id == 0)
                link.addtime = DateTime.Now;

            bool b = _bll.Save(link);
            if (b)
            {
                SaveLog(id == 0 ? "Add" : "edit");
                Utils.Alert(this.Page, "保存成功！");
                Utils.Redirect(this.Page, "list.aspx?type=" + type, false);
            }
        }
    }
}